package net.oschina.android.util;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

/**
 * @author HuangWenwei
 *
 * @date 2014年9月5日
 */
public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache {
	
	public static int getDefaultCacheSize(){
		final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
		final int cacheSize = maxMemory /8;
		return cacheSize;
	}
	
	/**
	 * @param maxSize
	 */
	public LruBitmapCache(int maxSize) {
		super(maxSize);
	}
	
	public LruBitmapCache(){
		this(getDefaultCacheSize());
	}

	@Override
	protected int sizeOf(String key, Bitmap value) {
		return value.getRowBytes() * value.getHeight() /1024;
	}

	@Override
	public Bitmap getBitmap(String url) {
		return get(url);
	}

	@Override
	public void putBitmap(String url, Bitmap bitmap) {
		put(url, bitmap);
	}
}
